﻿Imports System.Data.OleDb
Public Class _1CreateProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Protected Sub btn_submit_Click(sender As Object, e As EventArgs) Handles btn_submit.Click

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString & getSuffix(fup_picture.FileName)

        If fup_picture.HasFile Then
            fup_picture.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If

        Dim USerDOB As DateTime = New DateTime(CInt(ddl_year.SelectedValue), CInt(ddl_month.SelectedValue), CInt(ddl_day.SelectedValue))

        Dim continueUrl As String = "List.aspx"

        Dim sql As String = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Gender=@f3, DateOfBirth=@f4, ProfilePicture=@f5, Email=@f6, BasicInfo=@f7 WHERE DesignerID=@f8"
        Dim sql1 As String = "INSERT INTO DesignerTagUser (User_fkID, Tags_fkID) VALUES (@f9, @f10)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", USerDOB.Date)
        cmd.Parameters.AddWithValue("@f5", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", tb_email.Text)
        cmd.Parameters.AddWithValue("@f7", tb_basicInfo.Text)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)

        For Each cb As ListItem In cb_tags.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd1 As OleDbCommand = New OleDbCommand(sql1, conn)
                cmd1.Parameters.AddWithValue("@f9", User.Identity.Name)
                cmd1.Parameters.AddWithValue("@f10", cb.Value)
                cmd1.ExecuteNonQuery()
            End If
        Next

        cmd.ExecuteNonQuery()

        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect(continueUrl)
    End Sub

    Function getSuffix(ByRef fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class
